#!/bin/bash

# MySQL with Docker 部署脚本
echo ==================================================
echo Script startup for deploy Memcached
echo ==================================================

# Memcached的文件目录路径
MEMCACHED_DIRECTORY=/home/dockerimage-build/memcached
MEMCAEHED_DOCKERFILE_CONTENT="FROM daocloud.io/library/ubuntu:latest\n
                                MAINTAINER stevejrong\n
                                RUN apt-get update && apt-get install -y memcached
                                EXPOSE 11211\n
                                USER daemon\n
                                ENTRYPOINT memcached\n
                                CMD [\"-m\", \"512\"]"
# Memcached镜像库地址
MEMCACHED_DOCKER_REGISTRY=stevejrong-memcached
# Memcached镜像版本
MEMCACHED_DOCKER_VERSION=latest
# Memcached的Docker实例名称
MEMCACHED_DOCKER_INSTANCE_NAME=memcached-main
# Memcached的最大内存使用量
MEMCACHED_DOCKER_MAX_MEMORY_USAGE=512m
# Memcached物理机映射端口
MEMCACHED_PHYSICAL_MACHINE_PORT=11211
# Memcached虚拟机映射端口
MEMCACHED_VIRTUAL_MACHINE_PORT=11211

############################ installing start ############################
echo -e "-------------------- installing\n"

echo "-------------------- creating memcached directory"
cd /
mkdir -p -m 777 $MEMCACHED_DIRECTORY
echo -e "-------------------- create success.\n"

echo "-------------------- creating memcached dockerfile"
cd $MEMCACHED_DIRECTORY
echo -e "$MEMCAEHED_DOCKERFILE_CONTENT" > Dockerfile
echo -e "-------------------- create success.\n"

echo "-------------------- building memcached image"
docker build -t $MEMCACHED_DOCKER_REGISTRY .
echo -e "-------------------- build success.\n"

echo "-------------------- running memcached"
docker run --name $MEMCACHED_DOCKER_INSTANCE_NAME -m $MEMCACHED_DOCKER_MAX_MEMORY_USAGE -d -p $MEMCACHED_PHYSICAL_MACHINE_PORT:$MEMCACHED_VIRTUAL_MACHINE_PORT --network=host $MEMCACHED_DOCKER_REGISTRY:$MEMCACHED_DOCKER_VERSION
echo -e "-------------------- run success.\n"

echo -e "-------------------- install completed!\n"
############################ installing end ##############################

############################ validating start ############################
echo -e "-------------------- validating\n"

echo -e "-------------------- validating mysql run status\n"
docker ps -a | grep "memcached"
sleep 3

echo -e "-------------------- validate completed!\n"
############################ validating end ############################

echo =================================================
echo Deploy Memcached completed!
echo =================================================